package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.UserConsensusLog;
import com.sc.nft.mapper.UserConsensusLogMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * 共识度日志表(sc_user_consensus_log)数据DAO
 *
 * @author zxy
 * @since 2024-08-24 21:56:44
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class UserConsensusLogDao extends ServiceImpl<UserConsensusLogMapper, UserConsensusLog> {

    public Page<UserConsensusLog> getPage(Long userId,Long collectionId,Page page){
        LambdaQueryWrapper<UserConsensusLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserConsensusLog::getUserId,userId);
        wrapper.eq(UserConsensusLog::getCollectionId,collectionId);
        wrapper.orderByDesc(UserConsensusLog::getId);
        return this.page(page, wrapper);
    }

    public BigDecimal getTodaySumChange(Long userId,Long collectionId){
        QueryWrapper<UserConsensusLog> wrapper = new QueryWrapper<>();
        wrapper.select("ifnull(sum(consensus_num),0)");
        wrapper.eq("user_id",userId);
        wrapper.eq("collection_id",collectionId);
        DateTime now = DateTime.now();
        wrapper.between("create_time", DateUtil.beginOfDay(now),DateUtil.endOfDay(now));
        wrapper.eq("type",20);
        return getObj(wrapper,(x)->new BigDecimal(x.toString()));
    }
}